package com.qf.admin.servlet;

import com.qf.admin.domain.Sys_User;
import com.qf.admin.service.ISys_UserService;

import com.qf.admin.service.impl.Sys_UserService;
import com.qf.base.BaseServlet;
import com.qf.commons.Data;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/sys_user")
public class Sys_UserServlet extends BaseServlet {


     private ISys_UserService iSys_userService =new Sys_UserService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String methodName = req.getParameter("method");

        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addSys_user(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Object> map = new HashMap<>();
        String sname = req.getParameter("name");
        String password = req.getParameter("password");

        try{
            iSys_userService.addGetSys_UserData(sname,password);
            map.put("code", 1);
            map.put("msg", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", -1);
            map.put("msg", "fail");
        }

        try {
            sendJson(resp,map);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteSys_user(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Object> map = new HashMap<>();
        String id = req.getParameter("userId");
        try{
            iSys_userService.deleteSys_UserData(Integer.parseInt(id));
            map.put("code", 1);
            map.put("msg", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", -1);
            map.put("msg", "fail");
        }
        try {
            sendJson(resp,map);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void getAllSys_user(HttpServletRequest req, HttpServletResponse resp){


       List<Sys_User> sys_user = iSys_userService.getSys_User();

        try {
            sendJson(resp,sys_user);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void checkSys_user(HttpServletRequest req, HttpServletResponse resp){
        String userName = req.getParameter("usrName");
        String password = req.getParameter("password");
        Sys_User sysUser = iSys_userService.checkSys_UserNamePassword(userName, password);
        Data data = null;
        if (null != sysUser) {

            req.getSession().setAttribute("sys_user", sysUser);
            data = new Data(1, "success");
        } else{
            data = new Data(-1, "error");

        }
        try {
            sendJson(resp,data);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getUser(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, Object> map = new HashMap<>();
        String userId = req.getParameter("userId");
        System.out.println(userId);
        Sys_User sys_user = iSys_userService.getUser(Integer.parseInt(userId));
        if(null!=sys_user){
            map.put("code", 1);
            map.put("msg", "success");
        }else {
            map.put("code", -1);
            map.put("msg", "success");
        }
        try {
            sendJson(resp,sys_user);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
